package com.pankaj;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import com.pankaj.R;


public class xml_store {

	//private final String urlString="http://app.i-tee.nl/admin/getstore";
	//private final String urlString="http://urfamilyname.com/sptechdev/iteecms/admin/getstore";
    String res1;
    InputStream is;
    StringBuilder sb;
	//private Context mContext;
	

	String[] s_id;
	String[] s_name;
	String[] s_add1;
	String[] city;
	String[] img_url;
	String[] lat;
	String[] lon;
	String[] phone;
	String[] pincode;
	String[] country;
	String[] openingtime;
	String[] description;

    public xml_store(Context c) {
        ///mContext = c;
       
    }

    boolean mLoading;
    
    public void load() {
        mLoading = true;
        Document doc = null;
        HttpURLConnection urlConnection = null;
        URL url = null;
        try {

            url = new URL("http://iappcms.i-tee.nl/admin/getstore");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.connect();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(urlConnection.getInputStream());

        } catch (Exception e) {
        	System.out.println("e"+e.toString());
        }
        
        try {

            NodeList nodes = doc.getElementsByTagName("Store");

            s_id=new String[nodes.getLength()];
        	s_name=new String[nodes.getLength()];
        	s_add1=new String[nodes.getLength()];
        	city=new String[nodes.getLength()];
        	country=new String[nodes.getLength()];
        	img_url=new String[nodes.getLength()];
        	lat=new String[nodes.getLength()];
        	lon=new String[nodes.getLength()];
        	phone=new String[nodes.getLength()];
        	pincode=new String[nodes.getLength()];
        	openingtime=new String[nodes.getLength()];
        	description=new String[nodes.getLength()];
        	
            for (int i = 0; i < nodes.getLength(); i++) {
               Element element = (Element) nodes.item(i);
               NodeList name = element.getElementsByTagName("store_id");
               Element line = (Element) name.item(0);
               s_id[i]=getCharacterDataFromElement(line);
               //System.out.println("catcatcatmidmidmidmidName: " + getCharacterDataFromElement(line));

               NodeList title = element.getElementsByTagName("store_name");
               line = (Element) title.item(0);
               s_name[i]=getCharacterDataFromElement(line);
               //System.out.println("salary: " + getCharacterDataFromElement(line));

               NodeList add1 = element.getElementsByTagName("store_add1");
               line = (Element) add1.item(0);
               s_add1[i]=getCharacterDataFromElement(line);
               
               NodeList add2 = element.getElementsByTagName("pincode");
               line = (Element) add2.item(0);
               pincode[i]=getCharacterDataFromElement(line);

               NodeList cityname = element.getElementsByTagName("city");
               line = (Element) cityname.item(0);
               city[i]=getCharacterDataFromElement(line);
               
               NodeList cityname1 = element.getElementsByTagName("country");
               line = (Element) cityname1.item(0);
               country[i]=getCharacterDataFromElement(line);

               

               NodeList phone_no = element.getElementsByTagName("phone_no");
               line = (Element) phone_no.item(0);
               phone[i]=getCharacterDataFromElement(line);
               
               NodeList latitude = element.getElementsByTagName("latitude");
               line = (Element) latitude.item(0);
               lat[i]=getCharacterDataFromElement(line);

               NodeList longitude = element.getElementsByTagName("longitude");
               line = (Element) longitude.item(0);
               lon[i]=getCharacterDataFromElement(line);

               NodeList store_image = element.getElementsByTagName("store_image");
               line = (Element) store_image.item(0);
               img_url[i]=getCharacterDataFromElement(line);
               
               NodeList openingtime1 = element.getElementsByTagName("openingtime");
               line = (Element) openingtime1.item(0);
               openingtime[i]=getCharacterDataFromElement(line);

               NodeList description1 = element.getElementsByTagName("description");
               line = (Element) description1.item(0);
               description[i]=getCharacterDataFromElement(line);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        //new NetworkThread().start();
    }


      public static String getCharacterDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
           CharacterData cd = (CharacterData) child;
           return cd.getData();
        }
        return "";
      }
}